/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

/**
 *
 * @author Anton
 */

import javax.faces.event.ValueChangeEvent;
import com.icesoft.faces.context.effects.Effect;
import com.icesoft.faces.context.effects.Highlight;
import core.SuperSchool;
import java.util.Enumeration;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import web.SuperSchoolUserObject;
public class BaseBean
{
    protected Effect valueChangeEffect;

    protected DefaultTreeModel model;
    protected SuperSchoolUserObject selectedUserObject;
    protected int counter;
    protected SessionBean sessionBean;
    
    public BaseBean()
    {
        valueChangeEffect = new Highlight("#fda505");
        valueChangeEffect.setFired(true);
    }
    public void effectChangeListener(ValueChangeEvent event)
    {
        valueChangeEffect.setFired(false);
    }


    public Effect getValueChangeEffect()
    {
        return valueChangeEffect;
    }
    public void setValueChangeEffect(Effect valueChangeEffect)
    {
        this.valueChangeEffect = valueChangeEffect;
    }

     public SessionBean getSessionBean()
    {
        return sessionBean;
    }

    public void setSessionBean(SessionBean sessionBean)
    {
        this.sessionBean = sessionBean;
    }

    public DefaultTreeModel getModel() {
        return model;
    }

    public void setModel(DefaultTreeModel model) {
        this.model = model;
    }

    public SuperSchoolUserObject getSelectedUserObject() {
        return selectedUserObject;
    }

    public void setSelectedUserObject(SuperSchoolUserObject selectedUserObject)
    {
        this.selectedUserObject = selectedUserObject;
    }

    public void superSchoolNodeSelected(ActionEvent event) {

        String superSchoolId = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("superSchoolId");
        DefaultMutableTreeNode node = findTreeNode(superSchoolId);
        selectedUserObject = (SuperSchoolUserObject) node.getUserObject();
        valueChangeEffect.setFired(false);
    }

    protected DefaultMutableTreeNode addNode(DefaultMutableTreeNode parent,
                                           String title, int type, int id,
                                           SuperSchool superSchool)
    {
        DefaultMutableTreeNode node = new DefaultMutableTreeNode();        
        SuperSchoolUserObject userObject = new SuperSchoolUserObject(node, type, id, node.getPath());
        node.setUserObject(userObject); 
        userObject.setTreeObject(superSchool);
        if (title != null) {
            userObject.setText(title);
            userObject.setLeaf(false);
            userObject.setExpanded(false);
            node.setAllowsChildren(true);
        }
        else {
            userObject.setText(superSchool.getName());
            userObject.setLeaf(true);
            node.setAllowsChildren(false);
        }

        if (parent != null) {
            parent.add(node);
        }

        return node;
    }

    protected DefaultMutableTreeNode findTreeNode(String nodeId) {
        DefaultMutableTreeNode rootNode =
                (DefaultMutableTreeNode) model.getRoot();
        DefaultMutableTreeNode node;
        SuperSchoolUserObject tmp;
        Enumeration nodes = rootNode.depthFirstEnumeration();
        while (nodes.hasMoreElements()) {
            node = (DefaultMutableTreeNode) nodes.nextElement();
            tmp = (SuperSchoolUserObject) node.getUserObject();
            if (nodeId.equals(String.valueOf(tmp.getTreeId()))) {
                return node;
            }
        }
        return null;
    }
}